package com.task.core.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.task.common.annotation.DataScope;
import com.task.common.domain.info.KeyAndValue;
import com.task.common.exception.ServiceException;
import com.task.common.utils.SecurityUtils;
import com.task.common.utils.uuid.UUID;
import com.task.core.domain.DatasourceGroupInfo;
import com.task.core.dto.request.DatasourceGroupRequestDTO;
import com.task.core.mapper.ConversionsMapper;
import com.task.core.mapper.DatasourceGroupMapper;
import com.task.core.service.DatasourceGroupService;

/**
 * @author 迪迦.
 * @date 2024/11/4 17:25
 */
@Service("DatasourceGroupService")
public class DatasourceGroupServiceImpl implements DatasourceGroupService {

  @Resource
  private DatasourceGroupMapper datasourceGroupMapper;

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<DatasourceGroupInfo> getDatasourceGroupByPage(DatasourceGroupRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return datasourceGroupMapper.getDatasourceGroupByPage(request);
  }

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<KeyAndValue> getDatasourceGroupForOption(DatasourceGroupRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return datasourceGroupMapper.getDatasourceGroupForOption(request);
  }

  @Override
  public void addDatasourceGroup(DatasourceGroupRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    request.setCode(UUID.randomUUID().toString().replace("-", ""));
    datasourceGroupMapper.addDatasourceGroup(request);
  }

  @Override
  public void modifyDatasourceGroup(DatasourceGroupRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    Integer integer = datasourceGroupMapper.modifyDatasourceGroup(request);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
  }

  @Override
  public void deleteDatasourceGroup(String datasourceGroupCode, Long userId) {
    Integer integer = datasourceGroupMapper.deleteDatasourceGroup(datasourceGroupCode, userId);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
  }

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public DatasourceGroupInfo getDatasourceGroupDetailByCode(String datasourceGroupCode) {
    DatasourceGroupRequestDTO datasourceGroupRequestDTO = new DatasourceGroupRequestDTO();
    datasourceGroupRequestDTO.setCode(datasourceGroupCode);
    return datasourceGroupMapper.getDatasourceGroupDetailByCode(datasourceGroupRequestDTO);
  }
}
